import Vue from "vue";
import VueRouter from "vue-router";
import store from "../store/index"

Vue.use(VueRouter);

import home from "../views/home/home.vue"

const routes = [{
        path: "/",
        name: "Home",
        component: home,
        meta: {
            index: 1
        }
    },
    {
        path: "/category",
        name: "Category",
        component: () =>
            import ("../views/category/category.vue"),
        meta: {
            index: 1
        }

    },
    {
        path: "/shopcart",
        name: "Shopcart",
        component: () =>
            import ("../views/shopcart/shopcart.vue"),
        meta: {
            index: 1,
            auth: true
        }
    },
    {
        path: "/user",
        name: "User",
        component: () =>
            import ("../views/user/user.vue"),
        meta: {
            index: 1,
            auth: true
        }
    }, {
        path: '/login',
        name: 'Login',
        component: () =>
            import ("../views/login/login.vue"),
        meta: {
            index: 2
        }
    }, {
        path: "/product-list/:id",
        name: "productList",
        component: () =>
            import ("../views/productList/productList.vue"),
        meta: {
            index: 2
        }
    }, {
        path: "/product/:id",
        name: "productDetail",
        component: () =>
            import ("../views/productDetail/productDetail.vue")
    }, {
        path: "/profile",
        name: "profile",
        component: () =>
            import ("../views/login/loginOut.vue")
    }, {
        path: '/proxy',
        name: 'Proxy',
        component: () =>
            import ('../views/login/proxy.vue')
    }
];

const router = new VueRouter({
    mode: 'history',
    routes,
});

router.beforeEach((to, from, next) => {
    if (to.meta.auth) {
        if (store.getters.getIsLogin) {
            next()
        } else {
            next({ path: "/login" })
        }
    } else {
        next()
    }
})

export default router;